package com.yusys.jin.blog.configuration;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Component;

@Component
public class MenuConfig {
    private static Map<Integer, Map> map = null;

    static {

        map = loadMenu();
    }

    public static Map<Integer, Map> loadMenu() {
        System.out.println("开始加载菜单");
        Map<Integer, Map> firstMenu = null;
        SAXReader reader = new SAXReader();
        String path = System.getProperty("user.dir") + "\\src\\main\\resources\\static\\config\\menu.xml";
//		D:\Users\LENOVO\git\IndexForFreeDream\src\main\resources\static\config
        Document doc;
        try {
            doc = reader.read(new File(path));
            Element root = doc.getRootElement();
            List<Element> list = root.elements();
            firstMenu = new HashMap<Integer, Map>();
            for (Element e : list) {
                Integer key = Integer.parseInt(e.attributeValue("state"));
                List<Element> value = e.elements();
                Map<Integer, String> secondMenu = new HashMap<Integer, String>();
                for (Element a : value) {
                    Integer secondKey = Integer.parseInt(a.attributeValue("state"));
                    String secondValue = a.getText();
                    secondMenu.put(secondKey, secondValue);
                }
                firstMenu.put(key, secondMenu);
            }
        } catch (DocumentException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.err.println(firstMenu);
        System.err.println(path);
        return firstMenu;
    }

    public static Map<Integer, Map> getMap() {
        return map;
    }

    public static void setMap(Map<Integer, Map> map) {
        MenuConfig.map = map;
    }

}
